import Util from "../../utils/util";
import {getActivityDetail, signActivity} from "../../api/activity";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    parameter: {
      title: "",
      isBack: true,
    },
    opacity: 0,
    fields: {
      username: {title: "参与者姓名", required: true},
      age: {title: "年龄", required: true},
      mobile: {title: "手机号", required: true},
      address: {title: "所在地区", type: 'region', required: true},
      recommend_username: {title: "推荐机构/推荐人（如有）",},
    },

    batActive: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;

    this.setData({
      bottom: 30,
      activity_id: options.id || 0,
      navH: app.globalData.navHeight
    })

    if (Util.isEmpty(this.data.activity_id)) {
      if (getCurrentPages().length > 1) {
        Util.Tips({title: "数据错误"})
      } else {
        Util.Tips({title: "数据错误"}, {tab: 1, url: "/pages/activity/index"})
      }
      return;
    }


    Util.chekWxLogin().then(res => {
      that.setData({
        userInfo: res.userInfo || null
      })

      wx.showLoading({title: '正在加载...'});
      getActivityDetail(that.data.activity_id).then(res => {
        that.setData({...res.data});
      }).catch(errmsg => {
        Util.Tips({title: errmsg}, {tab: 3})
      }).finally(() => {
        wx.hideLoading();
      })
    }).then(() => {
      that.onLoadTabbar();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  inputName(e) {
    const key = e.currentTarget.id;
    let formData = this.data.formData || {},
      fields = this.data.fields;
    formData[key] = fields[key].default_val = e.detail.value;

    this.setData({
      fields: fields,
      formData: formData,
    })
  },

  bindRegionChange: function (e) { //选择地址
    const key = e.currentTarget.id;
    let formData = this.data.formData || {},
      fields = this.data.fields;
    fields[key].options = e.detail.value;
    if (e.detail.value[0] == e.detail.value[1]) {
      formData[key] = e.detail.value[1] + ' ' + e.detail.value[2];
    } else {
      formData[key] = e.detail.value.join(' ');
    }
    this.setData({
      fields: fields,
      formData: formData,
    })
  },

  formSubmit(e) {
    const that = this;

    if (!this.data.batActive)
      return false;

    let formData = this.data.formData || {},
      fields = this.data.fields;
    let msg = "";
    for (const k in fields) {
      if (!fields[k].required)
        continue;
      if (Util.isEmpty(formData[k])) {
        msg = fields[k].title + "必填";
        break;
      }
    }

    if (!Util.isEmpty(msg)) {
      return Util.Tips({title: msg, hideMask: true});
    }

    wx.showLoading({title: "正在提交..."});
    formData['activity_id'] = this.data.activity_id;
    signActivity(formData).then(res => {
      wx.setStorageSync("is_refresh", 1);
      wx.redirectTo({
        url: "../activity/success?id=" + this.data.activity_id
      })
    }).catch(errmsg => {
      Util.Tips({title: errmsg, hideMask: true});
    }).finally(() => {
      that.setData({
        batActive: true,
      })
    })
  },

  makePhoneCall(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.mobile
    });
  },

  onPageScroll(e) {
    const that = this
    let r = e.scrollTop / 100;
    this.setData({
      opacity: r >= 1 ? 1 : r
    })
  },

  goBack(e) {
    let pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack()
    } else {
      wx.switchTab({
        url: '/pages/activity/index'
      })
    }
  },

  onLoadTabbar: function () {//底部tabbar返回数据
    const that = this
    Util.checkIphone().then(res => {//检查是否是iphone x/11等
      that.setData({
        isIphoneX: res,
      })
    }).finally(() => {
      wx.createSelectorQuery().in(that).select('.weui-footer')
        .boundingClientRect(function (rect) {
          that.setData({
            bottom: parseInt(rect.height) + 30
          })
        }).exec()
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})